/**
 * 学生管理的单例
 */
var Stu = {
    id: "stuTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Stu.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'stuid', visible: false, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'stuname', align: 'center', valign: 'middle', sortable: true},
        {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true},
        {title: '入学年份', field: 'grade', align: 'center', valign: 'middle', sortable: true},
        {title: '学校', field: 'school', align: 'center', valign: 'middle', sortable: true},
        {title: '专业', field: 'major', align: 'center', valign: 'middle', sortable: true},
        {title: '地区', field: 'region', align: 'center', valign: 'middle', sortable: true},
        {title: '参与活动', field: 'actInfo', align: 'center', valign: 'middle', sortable: true},
        {title: '联系方式', field: 'contact', align: 'center', valign: 'middle', sortable: true},
        {title: 'QQ', field: 'qq', align: 'center', valign: 'middle', sortable: false},
        {title: '地址', field: 'address', align: 'center', valign: 'middle', sortable: false},
        {title: '工作情况', field: 'workstate', align: 'center', valign: 'middle', sortable: false}
]
    return columns;
};

/**
 * 检查是否选中
 */
Stu.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Stu.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加学生
 */
Stu.openAddStu = function () {
    var index = layer.open({
        type: 2,
        title: '添加学生',
        area: ['1080px', '850px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/stu/add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 */
Stu.openChangeStu = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改学生',
            area: ['1080px', '850px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/stu/edit/' + this.seItem.stuid+"/1"
        });
        this.layerIndex = index;
    }
};

/**
 * 删除学生
 */
Stu.delStu = function () {
    if (this.check()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/stu/remove", function () {
                Feng.success("删除成功!");
                Stu.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("stuid", Stu.seItem.stuid);
            ajax.start();
        };

        Feng.confirm("是否删除 " + Stu.seItem.stuname + "?",operation);
    }
};
/**
 * 详情查看
 */
Stu.detail = function (){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '活动详情',
            area: ['1080px', '850px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/stu/edit/' + this.seItem.stuid+"/0"
        });
        this.layerIndex = index;
    }
};
/**
 * 权限配置
 */
/*Role.assign = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/role/role_assign/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};*/

/**
 * 搜索学生
 */
Stu.search = function () {
    var queryData = {};
    queryData['stuName'] = $("#stuName").val();
    queryData['grade'] = $("#grade").val();
    queryData['school'] = $("#school").val();
    queryData['major'] = $("#major").val();
    queryData['actName'] = $("#actName").val();
    queryData['actYear'] = $("#actYear").val();
    Stu.table.refresh({query: queryData});
}
/**
 * 重置搜索
 */
Stu.resetSearch = function () {
    $("#stuName").val("");
    $("#grade").val("");
    $("#school").val("");
    $("#major").val("");
    $("#actName").val("");
    $("#actYear").val("");

    Stu.search();
}

$(function () {
    //日期初始化
    laydate.render({elem: '#grade',type: 'year'});
    laydate.render({
        elem: '#actYear'
        ,type: 'year'
    });

    var defaultColunms = Stu.initColumn();
    var table = new BSTable(Stu.id, "/stu/list", defaultColunms);
    table.setPaginationType("client");
    table.init();
    Stu.table = table;
});
